package android.tkb.activity.word;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.tkb.activity.word.SearchWordActivity.EditTextTextChangeListener;
import android.tkb.activity.word.SearchWordActivity.ListViewOnItemClickListener;
import android.tkb.dao.word.DatabaseHelper;
import android.tkb.model.word.Words;
import android.tkb.util.word.Contants;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

/*
 * 易错单词页面
 */
public class EasyWordActivity extends Activity {
	 private static final int NUM = 20;
	 private EditText et ;
	 private ListView lv ;
	 private Button back ;
	 
	 private String value = Contants.ER ;//给定一个初使值
	 
	 private Cursor cur;
	 private List<Words> result;
	 private ArrayList<Map<String,Object>> data;
	 
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.search_word);
	        
			 /* 从上一下物件接收内容++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
			 Intent intent = getIntent();
			 back = (Button) findViewById(R.id.back);
			
			 back.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(EasyWordActivity.this, MainActivity.class);
					startActivity(intent);
					finish();
				}
				 
			 });
	        et = (EditText) findViewById(R.id.search_key);
	        et.addTextChangedListener(new EditTextTextChangeListener());
	        
	        lv= (ListView) findViewById(R.id.search_lv);
	   	 
	        lv.setOnItemClickListener(new ListViewOnItemClickListener());
	        
	        
	        setTitle(R.string.word_easy_error);
	 }
	 
	 public void handleDate(String value){
		 
	     result = getDBDate(value,null);
	     data = loadData(result);
	     SimpleAdapter adapter  = new SimpleAdapter(this,data,R.layout.main_list,new String[]{"img","name"},new int[]{R.id.img,R.id.name});
	     lv.setAdapter(adapter);

	 }
	 
	 class EditTextTextChangeListener implements TextWatcher{

			@Override
			public void afterTextChanged(Editable arg0) {
				System.out.println("===afterTextChanged====");
				
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				System.out.println("===beforeTextChanged====");
				
			}

			@Override
			public void onTextChanged(CharSequence text, int arg1, int arg2,
					int arg3) {
				handleDate(text.toString());
				
			}
			 
		 }
	 
	 class ListViewOnItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			//updateDate();//更新计数
			//转到另一下页面------------------------------------------------------------------
			Intent intent  = new Intent();
			intent.setClass(EasyWordActivity.this, ShowWordMessageActivity.class);
			Words words3 = new Words();
			
			words3.setPrefeList(result);
			
			intent.putExtra("words", words3);
			intent.putExtra("index", position);//索引
			intent.putExtra("flag", value);
			
			startActivity(intent);
			finish();
		}
			
	}
	 
	 
	 public List<Words> getDBDate(String oriKey,String num){
		 
		 DatabaseHelper mOpenHelper = new DatabaseHelper(EasyWordActivity.this);
		 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		 
		 List<Words> resultList =new ArrayList<Words>();
		 
		 String[] whereStr = {oriKey +"%"};
		 
		 cur = db.query("WORDS", new String[]{"EN","JP","right_times","wrong_times"}, " en like ? and wrong_times >= 1", whereStr , null, null, "en asc", num);	
		 while (cur.moveToNext()) {
				Words wd = new Words();
				String en = cur.getString(cur.getColumnIndex("EN"));
				String ch = cur.getString(cur.getColumnIndex("JP"));
				wd.setEn(en);
				wd.setJp(ch);
				wd.setRightTimes(cur.getInt(cur.getColumnIndex("right_times")));
				wd.setWrongsTimes(cur.getInt(cur.getColumnIndex("wrong_times")));
				wd.setTempState("ER");//易错标志
				resultList.add(wd);
					
			 }
		 
		 cur.close();
		 db.close();
		 mOpenHelper.close();
		 return resultList;
	 }
	 
	 /**
	  * 组装数据
	  * @param wordList
	  * @return
	  */
	 public ArrayList<Map<String,Object>> loadData( List<Words> wordList){
    	ArrayList<Map<String,Object>> coll = new ArrayList<Map<String,Object>>();
    	Map<String,Object> item ;
    
    	 for (int i = 0; i < wordList.size(); i++) {//显示单 词文本作答案用处
			 Words type = (Words) wordList.get(i);
			 item =  new HashMap<String,Object> ();

			 item.put("img", R.drawable.er);
			 item.put("name", type.getEn());

			 coll.add(item);
    	 }
    	return coll;
    
    }
}
